﻿@typeparam TValue

<input type="@InputType" value="@CurrentValue" @onchange="OnChange" />

@code {
    [Parameter] public TValue? Value { get; set; }
    [Parameter] public EventCallback<TValue?> ValueChanged { get; set; }

    string? CurrentValue{ get; set; }

    Task OnChange(ChangeEventArgs e)
    {
        var value = e.Value;
        if(value is null)
        {
            return Task.CompletedTask;
        }

        var newValue = Convert.ChangeType(value, typeof(TValue));

        Value = (TValue)newValue;
        ValueChanged.InvokeAsync(Value);


        CurrentValue = BindConverter.FormatValue(value)?.ToString();

        return Task.CompletedTask;
    }

    string? InputType => Value switch
    {
        double or float or int or decimal => "number",
        DateOnly or DateTime or DateTimeOffset => "date",
        _ => "text"
    };
}
